/*
Fancy Mail Server

Copyright (C) 2007  Carlos Daniel Ruvalcaba Valenzuela <clsdaniel@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <libfmail.h>
#include <odkutils.h>

typedef ProtocolHandler * (*PROTO_LOAD)();
typedef AuthManager * (*AUTH_LOAD)();

class SimpleLoad : public LoadHandler{
public:
    int Dispatch(Socket *sock, ProtocolHandler *ph){
        if (ph)
            return ph->Handle(sock);
        return 0;
    }
};

int main(){
    BaseServer *serv;
    ProtocolHandler *pop3;
    ProtocolHandler *smtp;
    AuthManager *auth;
    SimpleLoad *lh;
    IPC *ipc;
    
    ipc =  IPC::CreateIPC("socket://localhost:1900");
    
    odkLibHandle *lib_pop3;
    odkLibHandle *lib_smtp;
    PROTO_LOAD load_pop3;
    PROTO_LOAD load_smtp;
    
    odkLibHandle *lib_auth;
    AUTH_LOAD load_auth;
    
    lib_pop3 = odk_library_open("libfmail-pop3");
    lib_smtp = odk_library_open("libfmail-smtp");
    
    load_pop3 = (PROTO_LOAD)odk_library_symbol(lib_pop3, "plugin_protocol_load");
    load_smtp = (PROTO_LOAD)odk_library_symbol(lib_smtp, "plugin_protocol_load");
    
    pop3 = load_pop3();
    smtp = load_smtp();
    
    lib_auth = odk_library_open("libfmail-dbauth");
    load_auth = (AUTH_LOAD)odk_library_symbol(lib_auth, "plugin_authman_load");
    
    auth = load_auth();
    
    lh = new SimpleLoad();
    
    serv = new BaseServer(pop3, 12001);
    
    serv->SetLoadhandler(lh);
    serv->Listen();
    
    while(1){
        serv->Accept();
    }
    
    return 0;
}
